﻿using Base.Service.Interface;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using HZL.Common;
using Newtonsoft.Json.Linq;

namespace Base.Api.Admin.Controllers
{
    [Route("api/[controller]/[action]")]
   /* [ApiExplorerSettings(GroupName = "WeChat")]*///分组
    [ApiController]
    public class WeChatController : BaseController
    {
        private readonly IUsertService _usertService;

        public WeChatController(Base.Service.Interface.IUsertService usertService)
        {
            _usertService = usertService;
        }

        /// <summary>
        /// （判断用户是否登录）请传输自己的appid和secret,,登录成功返回该用户的手机号
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        [HttpGet]
        [Filters.ActionLog(group = "微信", name = "微信验证是否登录")]
        //[Authorize]
        public IActionResult CheckOpenid(string code,string appid,string secret)
        {
            string url = $@"https://api.weixin.qq.com/sns/jscode2session?appid={appid}&secret={secret}&js_code={code}&grant_type=authorization_code";
            var result = Http.Get(url);
            JObject jo = (JObject)JsonConvert.DeserializeObject(result);//或者JObject jo = JObject.Parse(jsonText);
            string openid = jo["openid"].ToString();
            var result2 = _usertService.CheckOpenid(openid);
            return Ok(result2);
        }

        /// <summary>
        /// （登录）请传输自己的appid和secret,通过verify判断随机数是否正确，并给Uaccount绑定openid
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        [HttpGet]
        [Filters.ActionLog(group = "微信", name = "微信验证用户是否存在")]
        //[Authorize]
        public IActionResult CheckUsert(string code,string Uaccount,string verify,string appid,string secret)
        {
            string url = $@"https://api.weixin.qq.com/sns/jscode2session?appid={appid}&secret={secret}&js_code={code}&grant_type=authorization_code";
            var result = Http.Get(url);
            JObject jo = (JObject)JsonConvert.DeserializeObject(result);//或者JObject jo = JObject.Parse(jsonText);
            string openid = jo["openid"].ToString();
            var result2 = _usertService.CheckUsert(Uaccount, code, openid, verify);
            return Ok(result2);
        }


        /// <summary>
        /// 用户解绑openid（退出）
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        [HttpGet]
        [Filters.ActionLog(group = "微信", name = "微信解绑openid")]
        //[Authorize]
        public IActionResult TuichuOpenid(string Uaccount)
        {
            var result2 = _usertService.TuichuOpenid(Uaccount);
            return Ok(result2);
        }

        /// <summary>
        ///（获取随机码）根据UAccount获取数据库验证码，如果没有该用户则添加用户并返回随机码
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        [HttpGet]
        [Filters.ActionLog(group = "微信", name = "随机数")]
        //[Authorize]
        public IActionResult verification(string UAccount)
        {
            var result2 = _usertService.verification(UAccount);
            return Ok(result2);
        }

    }
}
